
package com.eworking.eim;

import java.util.UUID;

import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;

import com.eworking.eim.util.EIMLog;
import com.eworking.eim.util.HttpClientRquest;

public class DeviceCollector {
    
    /** 日志输出标签 */
    private static final String _TAG = DeviceCollector.class.getSimpleName();
    
    /** 是否已发送 */
    private static final String _IS_SEND = "sent";
    
    public static String getDeviceinfo(Context context) {
        JSONObject jsonObject = new JSONObject();
        try {
            // 操作系统版本
            jsonObject.put("version", Build.VERSION.RELEASE);
            // 设备厂家
            jsonObject.put("manufacturer", Build.MANUFACTURER);
            TelephonyManager localTelephonyManager = (TelephonyManager) context.getSystemService("phone");
            // 手机唯一编号
            jsonObject.put("imei", localTelephonyManager.getDeviceId());
            jsonObject.put("operator", localTelephonyManager.getNetworkOperatorName());
            // 应用版本号
            jsonObject.put("eworking.version", EIMManager.getInstance().getVersion());
            // 地理位置
            LocationManager locationManager = (LocationManager) context.getSystemService("location");
            Location location = locationManager.getLastKnownLocation("gps");
            if (location == null) {
                location = locationManager.getLastKnownLocation("network");
            }
            if (location != null) {
                // 经纬度
                jsonObject.put("loc.lat", location.getLatitude());
                jsonObject.put("loc.lng", location.getLongitude());
            } else {
                EIMLog.d(_TAG, "没有地理位置信息可以获取");
            }
            jsonObject.put("token", UUID.randomUUID());
            jsonObject.put("model", "android");
        } catch (Exception exception) {
            EIMLog.d(_TAG, "获取设备的信息失败:" + exception.getMessage());
        }
        return jsonObject.toString();
    }
    
    /**
     * 发送客户端设备信息到服务端
     * 
     * @param context
     */
    public static void sendDeviceInfo(final Context context) {
        // 是否已发送
        boolean isSend = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(_IS_SEND, false);
        if (isSend) {
            return;
        }
        EIMLog.d(_TAG, "发送设备及应用版本信息到服务器端");
        try {
            new Thread(new Runnable() {
                
                public void run() {
                    // 请求地址
                    String strURL = EIMConfig.getInstance().getStorageUrl();
                    if (!strURL.startsWith("http")) {
                        strURL = "http://" + strURL;
                    }
                    strURL = strURL + "/" + EIMConfig.getInstance().getEntCode().replaceFirst("#", "/") + "/devices";
                    try {
                        // 发送JSON内容
                        String strDeviceInfo = getDeviceinfo(context);
                        String strResponse = HttpClientRquest.post(strURL, strDeviceInfo, null);
                        if (strResponse.contains("uuid")) {
                            SharedPreferences.Editor localEditor =
                                PreferenceManager.getDefaultSharedPreferences(context).edit();
                            localEditor.putBoolean(_IS_SEND, true);
                            localEditor.commit();
                        }
                    } catch (Exception exception) {
                        EIMLog.e(_TAG, "发送设备及应用版本信息到服务器端失败，" + exception.getMessage());
                    }
                }
            }).start();
        } catch (Exception exception) {
            EIMLog.e(_TAG, "发送设备及应用版本信息到服务器端失败，" + exception.getMessage());
        }
    }
    
    public void sendUninstallPacket() {
    }
}
